# -*- coding:utf-8 -*-
'''
使用面向对向优化AirplaneFight 代码
'''
import  pygame
from pygame.locals import *
import time
import random

class Airplane(object):
    def __init__(self):
        # x = (480 - 46) * 0.5  # 飞机初始x
        # y = 700 - 57 - 30  # 飞机初始y
        self.space = 5;
        self.x = 0;
        self.y = 0;
        #保存子弹
        self.bullets = [];
        self.image = pygame.image.load("../AirplaneFightResources/life.png")

    #飞机添加到屏幕中
    def display(self,screen):
        screen.blit(self.image, (self.x, self.y))
        for bullet in self.bullets:
            if bullet.y < 0:
                self.bullets.remove(bullet)
            else:
                bullet.display(screen)
            # 发射炮弹

    # 发射炮弹
    def fire(self, screen):
        bullet = Bullet(self.x, self.y,'bullet1',10);
        self.bullets.append(bullet)

class HeroAirplane(Airplane):
    def __init__(self):
        # x = (480 - 46) * 0.5  # 飞机初始x
        # y = 700 - 57 - 30  # 飞机初始y
        self.space = 5;
        self.x = (480 - 46) * 0.5;
        self.y = 700 - 57 - 30;
        #保存子弹
        self.bullets = [];
        self.image = pygame.image.load("../AirplaneFightResources/life.png")

    #飞机添加到屏幕中
    def display(self,screen):
        screen.blit(self.image, (self.x, self.y))
        for bullet in self.bullets:
            bullet.display(screen)

    #飞机向左移动
    def left(self):
        self.x -= self.space;

    # 飞机向右移动
    def right(self):
        self.x += self.space;

    # 飞机向上移动
    def up(self):
        self.y -= self.space;

    # 飞机向下移动
    def down(self):
        self.y += self.space;

class EnemyAirplane(Airplane):
    def __init__(self):
        #重写父类init方法 Airplane._init_(self)
        super().__init__()
        self.image = pygame.image.load("../AirplaneFightResources/enemy1.png")
        self.direction = 'right';

    def move(self):
        if self.direction == 'left':
            self.x -= self.space;
            # self.y += 1;
        else:
            self.x += self.space;
            # self.y += 1;

        if self.x < 0:
            self.direction = 'right'
        elif self.x > (480 - 57):
            self.direction = 'left'

    #飞机添加到屏幕中
    def display(self,screen):
        screen.blit(self.image, (self.x, self.y))
        '''
        移除越界的炮弹方式 1 这种方式会出现漏删的情况
        nums = [11,22,33,44,55]
         for i in nums:
             if i == 22 or i == 33:
                nums.remove(i)
         最终结果可能是
         nums = [11,33,44,55]
         原因是：Python数组中移除一个元素后，会进行重新排序，导致i+1索引下的值，移动到i的位置，从而没有删除
         i+1位置上想删除的值。最终解决办法是：把想删除的元素添加到一个数组中，最后再删除。总之：不能再循环体中删除
        '''
        # for bullet in self.bullets:
        #     if bullet.y < 0:
        #         self.bullets.remove(bullet)
        #     else:
        #         bullet.display(screen)

        #移除越界的炮弹方式 2
        removeBullet = []
        for bullet in self.bullets:
            if bullet.y < 0:
                removeBullet.append(bullet)
            else:
                bullet.display(screen)

        #循环之后移除越界的炮弹
        for b in removeBullet:
            self.bullets.remove(b)

    def fire(self, screen):
        randomNum = random.randint(1,100);
        if randomNum == 30 or randomNum == 50:
            #使用随机数控制发射子弹的频率，不能使用sleep控制。但是用sleep控制时，会导致程序严重卡顿
            bullet = Bullet(self.x, self.y+83,'bullet2',-10);
            self.bullets.append(bullet)

class Bullet(object):
    def __init__(self,x,y,img,space):
        #5 11
        # 23 为飞机宽度的一半
        self.x = ((480 - 5) * 0.5)  if 0 else (x + 23 - 5);
        self.y = (700 - 11 - 57 - 30) if 0 else (y - 11 - 20);
        self.space = space;
        print(str(self.x))
        print(str(self.y))
        url = ("../AirplaneFightResources/%s.png"%(img));
        self.image = pygame.image.load(url)

    #子弹添加到屏幕中
    def display(self,screen):
        screen.blit(self.image, (self.x, self.y))
        #每一次都需要自动更改位置
        self.y -= self.space;

def main():
    #创建一个窗口
    screen = pygame.display.set_mode((480,700),0,0)
    #窗口添加一个背景
    background = pygame.image.load("../AirplaneFightResources/background.png")
    #创建一个飞机
    hero1 = HeroAirplane()

    #创建一个敌机
    enemy = EnemyAirplane()

    while True:
        screen.blit(background, (0, 0))
        #监听键盘点击事件
        observerEvent(hero1,screen)
        #飞机显示出来
        hero1.display(screen)
        #显示敌机
        enemy.display(screen)
        enemy.fire(screen)
        enemy.move()
        #更新屏幕信息
        pygame.display.update()

#监听键盘时间
def observerEvent(airplane,screen):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                airplane.left()
            elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                airplane.right()
            elif event.key == pygame.K_w or event.key == pygame.K_UP:
                airplane.up()
            elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                airplane.down()
            elif event.key == pygame.K_SPACE:
                airplane.fire(screen)

if __name__ == '__main__':
    #初始化pygame模块 如果pygame.init() 写在 main函数中时
    #事件监听会出现问题，由于pygame.init()一直在被初始化，所以会报错
    pygame.init()
    main()
